package com.chang.common.group;

import com.chang.common.ex.ValidateException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp-api
 * @description: 验证结果
 * @author: chang
 * @create: 2025-02-18 11h
 **/

/**
 * 验证结果
 */
public interface V {

    public static void validate(BindingResult result) {
        List<FieldError> errors = result.getFieldErrors();
        if (null != errors && errors.size() > 0) {
            Map<String, String> errorMap = new HashMap<>();
            //验证通过
            errors.forEach(el -> {
                errorMap.put(el.getField(), el.getDefaultMessage());
            });
            //有错误信息，抛出错误信息
            throw new ValidateException(errorMap);
        }
    }
}
